#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>

#include <QThread>
#include <QComboBox>
#include <QMenu>
#include <QMessageBox>
#include <QSpinBox>
#include <QScrollArea>


#include "zlgcan.h"
#include "plotdialog.h"
#include "historytable.h"


#include "MyQjson.h"

#include <QDir>
#include <QTextBrowser>

#include "windows.h"

#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>



#define  GET_BIT(Value,Bit)   ( (Value) & (1 << Bit) )

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

// 自定义按钮类
class CustomButton : public QPushButton {
    Q_OBJECT
public:
    explicit CustomButton(const QString &text, QWidget *parent = nullptr); // 构造函数
protected:
    void mousePressEvent(QMouseEvent *event) override; // 重写鼠标按下事件
 signals:
    void customContextMenuRequested(CustomButton *cBt,const QPoint &pos);
    void send_loadAction();
};

// 自定义标签类
class CustomLabel : public QLabel {
    Q_OBJECT

public:
    explicit CustomLabel(const QString &text,QWidget *parent = nullptr);

protected:
    void mousePressEvent(QMouseEvent *event) override;   // 重写鼠标按下事件
    void mouseReleaseEvent(QMouseEvent *event) override; // 重写鼠标释放事件

signals:
    // 用于显示当前变量的曲线
    // 暂不开发该功能
    void send_showGui(int32_t mylab_index);
    void send_disreset(int32_t mylab_index);
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
   // QWidget* myQwidgt;
    #define  NUM  (60)
    CustomLabel*    myLab_Name[NUM];
    QLineEdit* myLE_txt[NUM];
    QLabel*    WRT_myLab_Name[NUM];
    QLineEdit* WRT_myLE_txt[NUM];
    QLabel*    LAB_myLab_Name[NUM];
    QLineEdit* LAB_myLE_txt[NUM];


    QList<sValArry> ql_temp;
    QList<sValArry> Wrt_ql_temp;
    QList<sValArry> Lab_ql_temp;


    // 控制按钮变量[TOP Layout]
    QLabel *QLab_CANDEV = new QLabel("设备");
    QComboBox *QCom_CANDEV = new QComboBox();
    QComboBox *QCom_CANCHsel = new QComboBox();
    QLabel *QLab_BaudRate = new QLabel("波特率");
    QComboBox *QCom_Baud = new QComboBox();
    QPushButton *QPbtn_OPDEV = new QPushButton("打开CAN设备");
    QLabel *QLab_Config = new QLabel("配置文件");
    //QPushButton *QPbtn_LoadConfig = new QPushButton("载入配置");
    CustomButton  *QPbtn_LoadConfig = new CustomButton("载入配置");
    QComboBox *QCom_Config = new QComboBox();


    DEVICE_HANDLE MyCan_Handle;
    CHANNEL_HANDLE myChannel_Handle;



    #define DEV_USBCAN 2
    int32_t initCan_GY8507();
    void Can_Send_EX_GY(uint32_t id,uint8_t *Data);
    void Can_Send_EX(DEVICE_HANDLE Channel,uint32_t id,uint8_t *Data); //ZLG 发送

    ~MainWindow();

protected:


private slots:
    void onLineEditReturnPressed();
    void on_BTN_OpenDev();
    void on_BTN_LoadConfig();
    void on_R_BTN_LoadConfig();
    void on_CAN_DAT(ZCAN_Receive_Data *CANData,int num);
    void on_LoadLog();
    void on_send_disreset(int32_t index);
    void on_send_GUI(int32_t index);
    // 加载历史事件
    void on_HisEvent();

   void on_QCom_CANDEV_change();



private:
    Ui::MainWindow *ui;

    // 单滚动条: 即以整个界面作为滚动窗口
    // 三滚动条: 即三个功能视图单独进行滚动控制【待修复】
    #define  ONLY_QSCROLLAREA

    QWidget* myQwidgt  = new QWidget();

    // 这个窗口用于滚动条设置
    QWidget* sub1Qwidgt = new QWidget(myQwidgt);
    QWidget* sub2Qwidgt = new QWidget(myQwidgt);
    QWidget* sub3Qwidgt = new QWidget(myQwidgt);

    QVBoxLayout *top_layout   = new QVBoxLayout();
    QHBoxLayout *top_hlayout  = new QHBoxLayout();

#ifdef  ONLY_QSCROLLAREA
    QHBoxLayout *main_hlayout = new QHBoxLayout(sub1Qwidgt);
    //QHBoxLayout *main_hlayout = new QHBoxLayout();
#else
    QHBoxLayout *main_hlayout = new QHBoxLayout();
#endif


#ifdef  ONLY_QSCROLLAREA
    QVBoxLayout *v1_layout = new QVBoxLayout(); // top_layout->main_hlayout->v1_layout
    QVBoxLayout *v2_layout = new QVBoxLayout(); // top_layout->main_hlayout->v2_layout
    QVBoxLayout *v3_layout = new QVBoxLayout(); // top_layout->main_hlayout->v3_layout
#else
    QVBoxLayout *v1_layout = new QVBoxLayout(sub1Qwidgt);
    QVBoxLayout *v2_layout = new QVBoxLayout(sub2Qwidgt);
    QVBoxLayout *v3_layout = new QVBoxLayout(sub3Qwidgt);
#endif

    QScrollArea *v1_scrollArea = new QScrollArea; //滚动条
    QScrollArea *v2_scrollArea = new QScrollArea; //滚动条
    QScrollArea *v3_scrollArea = new QScrollArea; //滚动条


    void make_top_gui(); // 创建顶层界面
    void scan_configfile(QString ScanPath); //扫描配置文件
    void get_config2file(QString Dis_filePath,QString Wrt_filePath ,QString Lab_FilePath);
    void clear_layout(QLayout *layout);
    void get_DevParm();

    void CAN_SEND(sValArry sVal,DEVICE_HANDLE Channel,uint32_t mydat);

    void showCanDat(ZCAN_Receive_Data CANData,uint16_t sci_Addr,UINT8 *Byte, QList<sValArry> mylist,QString DataTime);

// ****    log弹窗相关变量 start ****
    qint64 filePtr = 0;
    qint64 lastfilePtr = 0;
    qint64  LenCode     = 0;
    int32_t indexPage = 1;

    QDialog *ctrlDiplayer = new QDialog();
    QDialog *ctrlAlarm    = new QDialog();


    QPushButton *pBtn_Last        = new QPushButton("上翻页");
    QPushButton *pBtn_Next        = new QPushButton("下翻页");
    QLabel      *mylab            = new QLabel("每页行数:");
    QSpinBox    *spinbox_Num      = new QSpinBox(this);
    QHBoxLayout  *loglayout       = new QHBoxLayout();
    QHBoxLayout  *alarm_layout    = new QHBoxLayout();

    void showContextMenu(CustomButton *cBtn,const QPoint &pos); // 右键菜单槽函数
    void showLog_DisCtrl();      // 显示log控制窗口

    void showAlarm_DisCtrl();    // 显示报警事件

    QComboBox *QCom_log = new QComboBox();
    void scan_logfile(QString ScanPath); // 扫描log文件

// ****    log弹窗相关变量 end ****


// ****    CanDebug弹窗相关变量 start ****
    QLabel      *LabSendTxt    = new QLabel("发送报文ID:(0x):");
    QLabel      *LabDatTxt     = new QLabel("数据:(0x):");
    QLineEdit   *le_SendID     = new QLineEdit();
    QLineEdit   *le_SendByte0  = new QLineEdit();
    QLineEdit   *le_SendByte1  = new QLineEdit();
    QLineEdit   *le_SendByte2  = new QLineEdit();
    QLineEdit   *le_SendByte3  = new QLineEdit();
    QLineEdit   *le_SendByte4  = new QLineEdit();
    QLineEdit   *le_SendByte5  = new QLineEdit();
    QLineEdit   *le_SendByte6  = new QLineEdit();
    QLineEdit   *le_SendByte7  = new QLineEdit();
    QPushButton *pBtn_Send  = new QPushButton("发送");

    QTextEdit   *QtextB_Recv = new QTextEdit();

    QVBoxLayout  *CanDebug_MainLayout = new QVBoxLayout();
    QHBoxLayout  *CanDebug_layout = new QHBoxLayout();
    QHBoxLayout  *CanDebugRecv_layout  = new QHBoxLayout();
    QDialog *ctrlCanDebug = new QDialog();

    void showCanDebug_DisCtrl(); // 显示CAN调试窗口
    void appendTextWithColor(QTextEdit *textEdit,const QString &text,QColor color);

// ****    CanDebug弹窗相关变量 end ****

// ****    Alarm弹窗相关变量 start ****
    HistoryTable *myhistorytable  =  new HistoryTable("历史报警事件",
                                                      QStringList() << "序号" << "时间"<<"事件",
                                                      {25,200,400},
                                                      false,
                                                      false);
    typedef  enum {
        WIND_REALY_TIME,    // 实时数据
        WIND_LOG_PALY,      // 数据回放
        WIND_GUI,           // 趋势图
        WIND_HISTORY_EVENT  // 历史事件显示
    }e_WindDisplayMode;
    e_WindDisplayMode enWindDispalyMode = WIND_REALY_TIME;

// ****    Alarm弹窗相关变量 end ****

// ****  COM Strart **************
    QSerialPort *m_port;
    QTimer QTIM_rx;
    QTimer QTIM_tx;
    uint8_t *Mydate ;

private slots:
    void onSerialPortReadyRead();
    void slot_update_rx_timer();
    void slot_update_tx_timer();

private :
    bool openSerialPort(const QString &portName, int baudRate);
    void closeSerialPort();
    void sendCommand(const char *data, int size);

// ****  COM End **************


signals:
     // 发送CAN通道句柄
     void sendhChanel(DEVICE_HANDLE hChannel);
};
#endif // MAINWINDOW_H
